<?php

class Buying extends CI_Controller {
    
    private $_auth;
    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if($this->_auth == FALSE) redirect ("backend/home");
    }
    
    public function index(){
        $data['data']['listpro']= $this->mbuying->fetchAll();
        $data['title'] = "Bảng mua hàng";
        $data['temp'] = "backend/buying/home";
        $this->load->view("backend/layout/index",$data);
    }
    
    public function add() {
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');           
            $this->form_validation->set_rules('data[content]', 'Nội dung', 'trim|required'); 
            if ($this->form_validation->run() == TRUE) {
                $this->mbuying->insert($post);
                redirect("backend/buying");
            }
        }
        $data['title'] = "Thêm mua hàng";
        $data['temp'] = "backend/buying/form";
        $this->load->view("backend/layout/index", $data);
    }

    public function delete($id) {
        $this->mbuying->delete($id);
        redirect("backend/buying");
    }

    public function deleteall() {
        $this->mbuying->deleteAll();
        redirect("backend/buying");
    }
    
    public function update($id){
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');           
            $this->form_validation->set_rules('data[content]', 'Nội dung', 'trim|required'); 
            if ($this->form_validation->run() == TRUE) {
                $this->mbuying->update($id,$post);
                redirect("backend/buying");
            }
        }
        $data['data']['pro'] = $this->mbuying->fetchOne($id);
        $data['title'] = "Sửa thông tin";
        $data['temp'] = "backend/buying/form";
        $this->load->view("backend/layout/index",$data);
    }
    
}
